package router

import (
	"fmt"
	"gforum/config"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

var r *gin.Engine
var api *gin.RouterGroup // 作为路由组路径的前缀

func SetUpRouter() *gin.Engine {
	r = gin.Default()

	// cors 配置跨域
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{fmt.Sprintf("http://%s:%s", config.Instance.Cors.Host, config.Instance.Cors.Port)},
		AllowMethods:     config.Instance.Cors.AllowMethods,
		AllowHeaders:     config.Instance.Cors.AllowHeaders,
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           config.Instance.Cors.MaxAge,
	}))

	api = r.Group("/api")

	setUpAuthRouter()
	setUpUserInfoRouter()
	setUpPostsRouter()
	setUpCommentsRouter()
	return r
}
